<?php
namespace app\admin\controller;


use app\admin\logic\CateLogic;
use app\BaseController;
use think\facade\View;
use app\admin\logic\GoodsLogic;
use app\admin\model\Gcategory;
use think\facade\Request;


class Cate extends BaseController
{
    //分类管理
    public function cateList()
    {
        //接值
        $param = Request::param();
        //处理参数
        $parent_id = GoodsLogic::newData($param);
        //查询语句
        $list = Gcategory::getCateGory($parent_id);
        //增加next_cate字段 用于判断是否可以点击下一分类
        $listNew = Gcategory::getNextCate($list);
        View::assign('list',$listNew);
        //分类下拉添加
        $cate = Gcategory::getGoodsCate();
        View::assign('cate',$cate);
        return View::fetch();
    }
    //分类添加
    public function cateAdd()
    {
        if (Request::isPost()){
            //接值
            $post = Request::post();
            //逻辑层添加数据
            $add = CateLogic::addCateInfo($post);
            //模型层添加数据
            list($res,$msg) = Gcategory::addCateInfo($add);
            //判断是否添加成功
            if ($res){
                $this->success($msg,url('cateList'));
            }else{
                $this->error($msg);
            }
        }

    }
    //分类修改
    public function cateEdit()
    {
        if (Request::isPost()){
            //接值
            $post = Request::post();
            //逻辑层处理修改数据
            $edit = CateLogic::editCateInfo($post);
            //模型层修改数据
            list($res,$msg) = Gcategory::editCateInfo([],$edit);
            if ($res){
                $this->success($msg,url('cateList'));
            }else{
                $this->error($msg);
            }
        }else{
            //接值
            $get = Request::get();
            //查询需要修改的数据
            $find = Gcategory::editCateInfo($get,[]);
            View::assign('find',$find);
        }

        return View::fetch();
    }
    //分类删除
    public function cateDel()
    {
        //接值
        $get = Request::get();
        list($res,$msg) = Gcategory::delCateInfo($get);
        if ($res){
            $this->success($msg,url('cateList'));
        }else{
            $this->error($msg);
        }
    }
}